package com.example.demo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.CommentMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.Comment;
import com.example.demo.pojo.User;
import com.example.demo.service.ICommentService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;


    public List<Comment> getCommentByQID(int id){
        QueryWrapper<Comment> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("question_id",id);
        return commentMapper.selectList(queryWrapper);
    }
    public void insertComment(Comment comment,int user_id){
        User user=userMapper.getUserById(user_id);
        String name=user.getUsername();
        Timestamp timestamp=new Timestamp(System.currentTimeMillis());
        comment.setUser_id(user_id);
        comment.setHp_url(user.getHp_url());
        comment.setUser_name(name);
        comment.setComment_time(timestamp);
        commentMapper.insert(comment);
    }
    public List<Comment> getAllComments(){
        return commentMapper.selectList(null);
    }

}
